/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DTO;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DatPLT
 */
public class ProjectAddSAXParser extends DefaultHandler{
    private Project project = null;
    private Date startDay = null;
    private Date endDay = null;

    private boolean bprojectManager = false;
    private boolean bprojectName = false;
    private boolean bdescription = false;
    private boolean bnumOfMinestone = false;
    private boolean bstartDay = false;
    private boolean bendDay = false;
    private boolean bstatus = false;

    private boolean bsyear = false;
    private boolean bsmonth = false;
    private boolean bsday = false;
    private boolean beyear = false;
    private boolean bemonth = false;
    private boolean beday = false;


    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equals("project")){
            project = new Project();
        }
        if(qName.equals("projectManager")){
            bprojectManager = true;
        }
        if(qName.equals("projectName")){
            bprojectName = true;
        }
        if(qName.equals("description")){
            bdescription = true;
        }
        if(qName.equals("numOfMinestone")){
            bnumOfMinestone = true;
        }
        if(qName.equals("startDay")){
            startDay = new Date();
            bstartDay = true;
        }
        if(qName.equals("year") && bstartDay){
            bsyear = true;
        }
        if(qName.equals("month") && bstartDay){
            bsmonth = true;
        }
        if(qName.equals("day") && bstartDay){
            bsday = true;
        }
        if(qName.equals("endDay")){
            endDay = new Date();
            bendDay = true;
        }
        if(qName.equals("year") && bendDay){
            beyear = true;
        }
        if(qName.equals("month") && bendDay){
            bemonth = true;
        }
        if(qName.equals("day") && bendDay){
            beday = true;
        }
        if(qName.equals("status")){
            bstatus = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length){
        String str = new String(ch, start, length);
        str = str.trim();
        if(bprojectManager){
            project.setProjectManager(str);
            bprojectManager = false;
        }
        if(bprojectName){
            project.setProjectName(str);
            bprojectName = false;
        }
        if(bdescription){
            project.setDescription(str);
            bdescription = false;
        }
        if(bnumOfMinestone){
            project.setNumOfMinestone(Integer.parseInt(str));
            bnumOfMinestone = false;
        }
        if(bsyear && bstartDay){
            startDay.setYear(Integer.parseInt(str));
            bsyear = false;
        }if(bstartDay){
            int a = 0;
        }
        if(bsmonth && bstartDay){
            startDay.setMonth(Integer.parseInt(str));
            bsmonth = false;
        }
        if(bsday && bstartDay){
            startDay.setDay(Integer.parseInt(str));
            bsday = false;
            bstartDay = false;
            project.setStartDay(startDay);
        }

        if(beyear && bendDay){
            endDay.setYear(Integer.parseInt(str));
            beyear = false;
        }
        if(bemonth && bendDay){
            endDay.setMonth(Integer.parseInt(str));
            bemonth = false;
        }
        if(beday && bendDay){
            endDay.setDay(Integer.parseInt(str));
            beday = false;
            bendDay = false;
            project.setEndDay(endDay);
        }
        if(bstatus){
            project.setStatus(str);
            bstatus = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equals("startDay")){            
            bstartDay = false;
        }
        if(qName.equals("endDay")){
            bendDay = false;            
        }
        if(qName.equals("project")){
            project.saveProject();
            project = null;
        }
    }
    
}
